<template>
    <div>
        <div class="switch-language">
            <el-dropdown size="large" :hide-timeout="50" placement="bottom-end" :hide-on-click="true">
                <Icon name="fa fa-globe" color="var(--el-text-color-secondary)" size="28" />
                <template #dropdown>
                    <el-dropdown-menu class="chang-lang">
                        <el-dropdown-item v-for="item in config.lang.langArray" :key="item.name" @click="editDefaultLang(item.name)">
                            {{ item.value }}
                        </el-dropdown-item>
                    </el-dropdown-menu>
                </template>
            </el-dropdown>
        </div>
        <div @contextmenu.stop="" id="bubble" class="bubble">
            <canvas id="bubble-canvas" class="bubble-canvas"></canvas>
        </div>
        <div class="login">
            <div class="login-box">
                <div class="head">
                    <!-- <img src="~assets/login-header.png" alt="" /> -->
                </div>
                <div class="form">

                   
                    <!-- <img class="profile-avatar" :src="fullUrl('/static/images/avatar.png')" alt="" /> -->
                    <div class="content">
                        <h2>亿乐云管理系统</h2>

                        <el-form @keyup.enter="onSubmitPre()" ref="formRef" :rules="rules" size="large" :model="form">
                            <el-form-item prop="mobile">
                                <el-input
                                    ref="mobileRef"
                                    type="text"
                                    clearable
                                    v-model="form.mobile"
                                    :placeholder="t('login.Please enter a mobile')"
                                >
                                    <template #prefix>
                                        <Icon name="fa fa-user" class="form-item-icon" size="16" color="var(--el-input-icon-color)" />
                                    </template>
                                </el-input>
                            </el-form-item>
                            <el-form-item prop="smsCode" class="checkCode">
                                <el-input
                                    ref="smsCodeRef"
                                    type="text"
                                    clearable
                                    v-model="form.smsCode"
                                    :placeholder="t('login.Please input a captcha code')"
                                >
                                    <template #prefix>
                                        <Icon name="fa fa-unlock-alt" class="form-item-icon" size="16" color="var(--el-input-icon-color)" />
                                    </template>
                                </el-input>
                                <el-button
                                    class="checkCodebutton"
                                    @click.stop="sendVerificationCode"
                                    size="mini"
                                    type="primary"
                                    style="margin-left: 10px"
                                    v-if="state.canSendSms"
                                >{{ t('login.Get captcha') }}</el-button>
                                <el-button
                                    class="checkCodebutton"
                                    size="mini"
                                    type="primary"
                                    style="margin-left: 10px"
                                    v-if="!state.canSendSms"
                                    disabled
                                >{{ state.timeCount }}</el-button>
                            </el-form-item>
                            <el-checkbox v-model="form.keep" :label="t('login.Hold session')" size="default">保持会话</el-checkbox>
                            <el-form-item>
                                <el-button
                                    :loading="state.submitLoading"
                                    class="submit-button"
                                    round
                                    type="primary"
                                    size="large"
                                    @click="onSubmitPre()"
                                >
                                    {{ t('login.Sign in') }}
                                </el-button>
                            </el-form-item>
                        </el-form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { onMounted, onBeforeUnmount, reactive, ref, nextTick } from 'vue'
import * as pageBubble from '/@/utils/pageBubble'
import type { FormInstance, InputInstance } from 'element-plus'
import { useI18n } from 'vue-i18n'
import { editDefaultLang } from '/@/lang/index'
import { useConfig } from '/@/stores/config'
import { useAdminInfo } from '/@/stores/adminInfo'
import { sendSms, telLogin } from '/@/api/backend'
import { buildValidatorData } from '/@/utils/validate'
import router from '/@/router'
import clickCaptcha from '/@/components/clickCaptcha'
import toggleDark from '/@/utils/useDark'
import { fullUrl } from '/@/utils/common'
import { adminBaseRoutePath } from '/@/router/static/adminBase'
// let timer: number

const config = useConfig()
const adminInfo = useAdminInfo()
toggleDark(config.layout.isDark)

const formRef = ref<FormInstance>()
const mobileRef = ref<InputInstance>()
const smsCodeRef = ref<InputInstance>()
const state = reactive({
    showCaptcha: false,
    submitLoading: false,
    canSendSms : true,
    timeCount : 0,
    timer : null,
})
const form = reactive({
    mobile: '',
    smsCode: '',
    keep: false,
    captchaInfo: '',
})

const { t } = useI18n()

// 表单验证规则
const rules = reactive({
    mobile: [buildValidatorData({ name: 'required', message: t('login.Please enter a mobile') }), buildValidatorData({ name: 'mobile' })],
    smsCode: [buildValidatorData({ name: 'required', message: t('login.Please input a captcha code') }), buildValidatorData({ name: 'smsCode' })],
})

const focusInput = () => {
    if (form.mobile === '') {
        mobileRef.value!.focus()
    } else if (form.smsCode === '') {
        smsCodeRef.value!.focus()
    }
}

onBeforeUnmount(() => {
    clearTimeout(state.timer)
    pageBubble.removeListeners()
})

const onSubmitPre = () => {
    formRef.value?.validate((valid) => {
        if (valid) {
             onSubmit()
        }
    })
}

const sendVerificationCode = () => {
    if (form.mobile === '') {
        mobileRef.value!.focus()
    } else {
        let TIME_COUNT = 60;
        if (!state.timer) {
            state.timeCount = TIME_COUNT;
            state.canSendSms = false;
            state.timer = setInterval(() => {
                if (state.timeCount > 0 && state.timeCount <= TIME_COUNT) {
                    state.timeCount--;
                } else {
                    state.canSendSms = true;
                    clearInterval(state.timer);
                    state.timer = null;
                }
            }, 1000);

            sendSms('post', {tel: form.mobile, smsType:1})
            .then((res) => {
            })
            .finally(() => {
            })
        }
    }
}

const onSubmit = () => {
    state.submitLoading = true
    telLogin('post', {tel: form.mobile, checkCode: form.smsCode})
        .then((res) => {
            adminInfo.dataFill(res.data)
            router.push({ path: adminBaseRoutePath })
        })
        .finally(() => {
            state.submitLoading = false
        })
}
</script>

<style scoped lang="scss">
.switch-language {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 1;
}
.bubble {
    overflow: hidden;
    background: url(/@/assets/logon.jpg) repeat;
    height: 100vh;
    background-repeat: no-repeat;
    background-size: cover;
}
.form-item-icon {
    height: auto;
}
.login {
    position: absolute;
    top: 0;
    display: flex;
    width: 100vw;
    height: 100vh;
    align-items: center;
    justify-content: center;
    .login-box {
        overflow: hidden;
        width: 430px;
        padding: 0;
        background: var(--ba-bg-color-overlay);
        margin-bottom: 80px;
        border-radius: 8px;
    }
    .head {
        background: #ccccff;
        img {
            display: block;
            width: 430px;
            margin: 0 auto;
            user-select: none;
        }
    }
    .form {
        position: relative;
        .profile-avatar {
            display: block;
            position: absolute;
            height: 100px;
            width: 100px;
            border-radius: 50%;
            border: 4px solid var(--ba-bg-color-overlay);
            top: -50px;
            right: calc(50% - 50px);
            z-index: 2;
            user-select: none;
        }
        .content {
            padding: 20px 40px 40px 40px;
        }
        .submit-button {
            width: 100%;
            letter-spacing: 2px;
            font-weight: 300;
            margin-top: 15px;
            --el-button-bg-color: var(--el-color-primary);
        }
    }
}

.checkCodebutton {
    position: absolute;
    right: 3px;
}

@media screen and (max-width: 720px) {
    .login {
        display: flex;
        align-items: center;
        justify-content: center;
        .login-box {
            width: 340px;
            margin-top: 0;
        }
    }
}
.chang-lang :deep(.el-dropdown-menu__item) {
    justify-content: center;
}
.content :deep(.el-input__prefix) {
    display: flex;
    align-items: center;
}

.content h2{
    text-align: center;
    margin-bottom: 30px;
}

// 暗黑样式
@at-root .dark {
    .bubble {
        background: url(/@/assets/bg-dark.jpg) repeat;
    }
    .login {
        .login-box {
            background: #161b22;
        }
        .head {
            img {
                filter: brightness(61%);
            }
        }
        .form {
            .submit-button {
                --el-button-bg-color: var(--el-color-primary-light-5);
                --el-button-border-color: rgba(240, 252, 241, 0.1);
            }
        }
    }
}
@media screen and (max-height: 800px) {
    .login .login-box {
        margin-bottom: 0;
    }
}
</style>
